package com.ymt.reliable.message.exceptionretry.task;

import com.ymt.reliable.message.domain.TransactionMessage;
import com.ymt.reliable.message.exceptionretry.config.MessageRetryConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

public class TaskFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskFactory.class);
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Task createTask(TransactionMessage message, Long delayTime) {
        Task task = new Task(message, delayTime);
        return task;
    }

    public static Task createTask(TransactionMessage message, MessageRetryConfig config) {
        Long delayTime = config.getDelayTime(message.getMessageSendTimes() + 1);
        Task task = new Task(message, delayTime);

        LOGGER.debug(">>> 延迟重试任务创建成功,taskId:{},lastPushTime:{},submitTime:{},剩余时间:{}分钟",
                task.getTaskId(),
                DATE_FORMAT.format(task.getMessage().getLastPushTime()),
                DATE_FORMAT.format(task.getSubmitTime()),
                task.getDelay(TimeUnit.MILLISECONDS) / 1000L * 60L);
        return task;
    }
}
